
/*
 * Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

import { Response, dialog, interfaces, Intent } from 'ask-sdk-model';
import { generateSlotsFromApiRequest, getRequestType } from '../../../util/RequestEnvelopeUtils';
import { CustomSkillRequestHandler } from './CustomSkillRequestHandler';
import { HandlerInput } from './HandlerInput';

import APIInvocationRequest = interfaces.conversations.APIInvocationRequest;
import APIRequest = interfaces.conversations.APIRequest;

const ACDL_AUTOGEN_NAMESPACE:string = "com.amazon.autogenerated";
const SPLIT_CHAR:string = "_";

export class DelegateToIntentHandler implements CustomSkillRequestHandler {

    canHandle(input : HandlerInput) : boolean {
        if (getRequestType(input.requestEnvelope) !== 'Dialog.API.Invoked') {
            return false;
        }
        if (!(input.requestEnvelope.request as APIInvocationRequest).apiRequest
            || !(input.requestEnvelope.request as APIInvocationRequest).apiRequest.name) {
            return false;
        }
        const apiName: string = (input.requestEnvelope.request as APIInvocationRequest).apiRequest.name;
        if (!apiName.startsWith(ACDL_AUTOGEN_NAMESPACE)) {
            return false;
        }
        return true;
    }

    handle(input: HandlerInput): Response | Promise<Response> {
        const apiRequest:APIRequest = (input.requestEnvelope.request as APIInvocationRequest).apiRequest;
        const apiName: string = apiRequest.name;
        const intentName: string = (apiName.substring(apiName.indexOf(SPLIT_CHAR) + 1));

        const directiveType: ('Dialog.DelegateRequest') = "Dialog.DelegateRequest";
        const delegationTarget: string = "skill";
        const updatedRequestType: ("Dialog.InputRequest" | "IntentRequest") = "IntentRequest";
        const delegationPeriod:dialog.DelegationPeriod = {
            until: 'EXPLICIT_RETURN'
        };

        const intent:Intent = {
            name: intentName,
            confirmationStatus: 'NONE',
            slots: generateSlotsFromApiRequest(apiRequest)
        };
        const updatedRequest:dialog.UpdatedRequest = {
            type: updatedRequestType,
            intent
        };
        const delegateRequestDirective:dialog.DelegateRequestDirective = {
            type: directiveType,
            target: delegationTarget,
            period: delegationPeriod,
            updatedRequest
        };

        return input.responseBuilder.addDirective(delegateRequestDirective).getResponse();
    }
}
